//
//  UILabel+Style.m
//  MasonryDemo
//
//  Created by xieyingze on 16/11/9.
//  Copyright © 2016年 xieyingze. All rights reserved.
//

#import "UILabel+Style.h"

#define kUI_HEIGHT      [UIScreen mainScreen].bounds.size.height
#define kUI_WIDTH       [UIScreen mainScreen].bounds.size.width

@implementation UILabel (Style)

#pragma mark - 设置标签的属性

- (void)setLabelStyle:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont texrAlignment:(NSTextAlignment)textAlignment {
    self.text = text;
    self.textColor = textColor;
    self.font = textFont;
    self.textAlignment = textAlignment;
}

- (void)setLabelText:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)textFont {
    self.text = text;
    self.textColor = textColor;
    self.font = textFont;
}

#pragma mark - 设置标签AttributedString

- (void)setLabelAttributedTextWithString:(UIColor *)color range:(NSRange)range {
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:self.text];
    [attStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    self.attributedText = attStr;
}

#pragma mark - 获取标签文字长度

+ (CGFloat)labelSpaceHeight:(NSString *)string width:(CGFloat)width font:(CGFloat)fontSize {
    NSMutableParagraphStyle *paragphStyle = [[NSMutableParagraphStyle alloc] init];
    
    paragphStyle.lineSpacing = 5;
    paragphStyle.firstLineHeadIndent = 0.f;
    paragphStyle.hyphenationFactor = 0.f;
    paragphStyle.paragraphSpacingBefore = 0.f;
    
    NSDictionary *dic = @{
                          NSFontAttributeName:[UIFont systemFontOfSize:fontSize],
                          NSParagraphStyleAttributeName:paragphStyle,
                          NSKernAttributeName:@1.0f
                          };
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, kUI_HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return size.height;
}

/**
 设置标签文字的间隔
 
 @param lineSpacing 间隔
 */
- (void)labelParagraphStyleWithLineSpacing:(CGFloat)lineSpacing {
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.text];
    NSMutableParagraphStyle *papragraphStyle = [[NSMutableParagraphStyle alloc] init];
    papragraphStyle.lineSpacing = lineSpacing;
    papragraphStyle.alignment = NSTextAlignmentCenter;
    [attrStr addAttribute:NSParagraphStyleAttributeName value:papragraphStyle range:NSMakeRange(0, attrStr.length)];
    self.attributedText = attrStr;
    [self sizeToFit];
}

#pragma mark - 行间距、字间距

+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
}

+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

@end
